package com.qf.bootmr.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author cml
 * @create 2021/6/2 8:47
 */
public class SmsUtil {

    @Value("${aliyun.sms.accessKeyId}")
    private static String accessKeyId;

    @Value("${aliyun.sms.accessSecre")
    private static String accessSecre;

    @Value("${aliyun.sms.singname}")
    private static String singName;

    public static String SendMs(String phoneNumber) {
        System.out.println(accessKeyId + "  " + accessSecre + " " + singName);
        DefaultProfile profile = DefaultProfile.getProfile("cn-chengdu", "LTAI5tBD7Yr5SqPXGbtBvETx", "xoQecEfUEYVcgSX4JevIlEhNJISLjW");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "阳阳商城");
        request.putQueryParameter("TemplateCode", "SMS_205409285");

        final String code = UUID.randomUUID().toString().substring(0, 4);
        HashMap<String, String> hashMap = new HashMap<>(1);
        hashMap.put("code",code);


        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(hashMap));
//        request.putQueryParameter("SmsUpExtendCode", "15125");
//        request.putQueryParameter("OutId", "41515");
        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success ? code : null;
        }catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }
}
